<?php

class CommentableBehavior extends ModelBehavior {

	var $model;
	
	var $defaults = array(
	
		'commentModel' => 'Comments.Comment',
		'dependent' => false
	
	);
	
	var $settings = array();

	function setup($model, $settings) 
	{
		$this->model = $model;
		$settings[$this->model->alias] = am($this->defaults, $settings);
		$this->settings = $settings;
		extract($settings[$this->model->alias]);
		if($commentModel == 'Comments.Comment')
		{
			
			$foreignKey = 'entity_id';
			$conditions = array('Comment.type' => $this->model->alias);
		}
		else
		{
			$foreignKey = Inflector::underscore($this->model->alias).'_id';
			$conditions = '';
		}
		$commentModelAlias = end(explode('.', $commentModel));
		$commentModelObj = cri($commentModel);
		$this->model->bindModel(array(
			'hasMany' => array(
				$commentModelAlias => array(
					'className' => $commentModel,
					'foreignKey' => $foreignKey,
					'dependent' => $dependent,
					'conditions' => $conditions,
					'fields' => '',
					'order' => '',
					'limit' => '',
					'offset' => '',
					'exclusive' => '',
					'finderQuery' => '',
					'counterQuery' => ''
				))
		), false);
		
		//prd($this->model);
		
		
		//$entities = ;
		$currentModelClassName = $this->model->className;
		
		$counterScope = array();
		if(isset($commentModelObj->belongsTo[$currentModelClassName]['counterScope']))
		{
			$counterScope = $commentModelObj->belongsTo[$currentModelClassName]['counterScope'];
		}
		
		$commentModelObj->bindModel(array('belongsTo' => array(
			$this->model->alias => array(
				'className' => $currentModelClassName,
				'counterCache' => true,
				'counterScope'=>$counterScope
			)
		)), false);
		
	}
	
	function getCommentModel($model)
	{
		extract($this->settings[$model->alias]);
		return cri($commentModel);
	}





}

?>